<?php

require_once '../include/common.inc.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

getMyParam('action');
getMyParam('start_date');
getMyParam('end_date');
getMyParam('uid');


if ($start_date && $end_date && $uid != '') {
    $filename = 'browse_tracking_' . $uid . '_' . date('Ymd',$start_date) . '_' . date('Ymd',$end_date) . '.xls';
} elseif ($uid != '') {
    $filename = 'browse_tracking_' . $uid . '.xls';
} elseif ($start_date && $end_date) {
    $filename = 'browse_tracking_' . date('Ymd',$start_date) . '_' . date('Ymd',$end_date) . '.xls';
} else {
    $filename = 'browse_tracking.xls';
}


if ($action == "export") {
    header("Content-type:application/vnd.ms-excel");
    header("Accept-Ranges:bytes");
    header("Content-Disposition:attachment;filename=".$filename);


    $sql_where = " WHERE `date` > '$start_date' AND `date` < '$end_date' AND `uid` = '$uid' ";
    
    $sql = "SELECT `id`, `name` FROM `category`";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $categroy_ary[$rs['id']] = $rs['name'];
    }
    $categroy_ary[1001] = 'New Items';

    $sql = "SELECT `id`, `name` FROM `inventory`";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $inventory_ary[$rs['id']] = $rs['name'];
    }


    $product_id_ary = array();
    $product_ary = array();
    $code_ary = array();

    $sql = "SELECT `id`, `uid`, `action`, `aid`, `details`, `date` FROM `browse_tracking` $sql_where ORDER BY `date` DESC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        if ($rs['action'] == 3 || $rs['action'] == 4) {
            $product_id_ary[] = $rs['aid'];
        }
    }

    if (!empty($product_id_ary)) {
        $product_id_str = implode(",", $product_id_ary);
        $sql_tmp = "select `id` ,`name`, `code` from `product` where `id` IN (" . $product_id_str . ")";
        $query_tmp = $db->query($sql_tmp);
        while($rs_tmp = $db->fetch_array($query_tmp)) {
            $product_ary[$rs_tmp['id']] = $rs_tmp['name'];
            $code_ary[$rs_tmp['id']] = $rs_tmp['code'];
        }
    }


    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        switch ($rs['action']) {
            case 1:
                $details = '';
                break;
            case 2:
                $details = '';
                break;
            case 3:
                $id_str = $rs['details'];
                if (empty($id_str)) {
                    $details = 'Products>' . $product_ary[$rs['aid']] . '(' . $code_ary[$rs['aid']] . ')';
                } else if (strstr($id_str, '||')) {
                    list($id1, $id2) = explode("||", $id_str);
                    $details = 'Products>' . $categroy_ary[$id1] . '>' . $categroy_ary[$id2] . '>' . $product_ary[$rs['aid']] . '(' . $code_ary[$rs['aid']] . ')';
                } else {
                    $id1 = $id_str;
                    $details = 'Products>' . $categroy_ary[$id1] . '>' . $product_ary[$rs['aid']] . '(' . $code_ary[$rs['aid']] . ')';
                }
                break;
            case 4:
                $id_str = $rs['details'];
                if (strstr($id_str, '||')) {
                    list($id1, $id2) = explode("||", $id_str);
                    $details = 'Inventory>' . $inventory_ary[$id1] . '>' . $categroy_ary[$id2] . '>' . $product_ary[$rs['aid']] . '(' . $code_ary[$rs['aid']] . ')';
                } else {
                    $id1 = $id_str;
                    $details = 'Inventory>' . $inventory_ary[$id1] . '>' . $product_ary[$rs['aid']] . '(' . $code_ary[$rs['aid']] . ')';
                }
                break;
            case 5:
                $details = 'Tradeshow>' . $rs['details'];
                break;
            case 6:
                $details = "Key: " . $rs['details'];
                break;
            case 7:
                $details = $rs['details'];
                break;
            default:
                $details = '';
                break;
        }

        $out_ary[] = array('date'=>date('Y-m-d', $rs['date']), 'time'=>date('H:i:s', $rs['date']), 'web_pages'=>$browse_tracking_ary[$rs['action']], 'details'=>$details);
    }

    $data = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>Untitled Document</title>
<style>
<!--
@page
	{margin:1.0in .75in 1.0in .75in;
	mso-header-margin:.5in;
	mso-footer-margin:.5in;}
br
	{mso-data-placement:same-cell;}
.style0
	{mso-number-format:General;
	text-align:general;
	vertical-align:bottom;
	white-space:nowrap;
	mso-rotate:0;
	mso-background-source:auto;
	mso-pattern:auto;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	mso-protection:locked visible;
	mso-style-id:0;}
td
	{mso-style-parent:style0;
	padding-top:1px;
	padding-right:1px;
	padding-left:1px;
	mso-ignore:padding;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	text-align:general;
	vertical-align:bottom;
	mso-background-source:auto;
	mso-pattern:auto;
	mso-protection:locked visible;
	white-space:nowrap;
	mso-rotate:0;}
.xl24
	{mso-style-parent:style0;
	font-size:8.0pt;
	font-family:Arial, sans-serif;
	mso-font-charset:0;}
.xl25
	{mso-style-parent:style0;
	font-size:8.0pt;
	font-weight:700;
	font-family:Arial, sans-serif;
	mso-font-charset:0;}
.xl26
	{mso-style-parent:style0;
	font-weight:700;}
ruby
	{ruby-align:left;}
rt
	{color:windowtext;
	font-size:9.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	mso-font-charset:136;
	mso-char-type:none;
	display:none;}
-->
</style>
</head>

<body>
<table width="100%" border="1" cellspacing="0" cellpadding="5">
<tr>
<td class=xl25>Date</td>
<td class=xl25>Time</td>
<td class=xl25>Web Pages</td>
<td class=xl25>Details</td>
</tr>';

    foreach($out_ary as $v) {
        $data .= "<tr>
					  <td class=xl24>" . $v['date'] . "</td> 
					  <td class=xl24>" . $v['time'] . "</td> 
					  <td class=xl24>" . $v['web_pages'] . "</td> 
					  <td class=xl24>" . $v['details'] . "</td></tr>";
    }


    $data .= "
</table>
</body>
</html>";
    echo $data;
}

?>